/*lic*/
#ifndef __BSP_SPI_INIT_HELPER_H__
#define __BSP_SPI_INIT_HELPER_H__

#include "main.h"

// spi状态
#define SPI_INITED 0x01

// spi传送阶段
#define SPI_TRANSLATE_IDLE 0x00 // SPI空闲状态, 这个状态并不用来传输
#define SPI_TRANSLATE_CMD 0x01 // 传送命令
#define SPI_TRANSLATE_SEND 0x02 // 发送数据
#define SPI_TRANSLATE_RECV 0x03 // 接收数据

typedef struct __Tag_Spi_Transport_Def {
	struct rt_semaphore spi_transport_sem;
	struct rt_mutex spi_lock;
	uint8_t spi_status;
	uint8_t spi_translate_status;
	
	size_t spi_cmd_cache_size; // 缓冲区尺寸
	size_t spi_data_cache_size;
	uint8_t spi_cmd_buf[16];
	uint8_t spi_data_buf[4096];
	
	// SPI收发尺寸
	uint8_t cmd_size;
	size_t send_size;
	size_t recv_size;
	
	CM_DMA_TypeDef *cm_dma; // DMA号
	uint8_t ch_dma; // DMA通道号
	uint32_t aos_tag; // AOS触发目标
	en_event_src_t aos_send_src_event; // 命令发送触发事件
	
	CM_SPI_TypeDef *spi_cm;
	
	rt_err_t (*Spi_Start_Wait)(struct __Tag_Spi_Transport_Def* obj);
	rt_err_t (*Spi_Set_Band)(struct __Tag_Spi_Transport_Def *obj, uint32_t u32BaudRatePrescaler);
}Spi_Transport_Def;

//typedef struct __Tag_Spi_Exchange_Def {
//	size_t spi_tx_size;
//	size_t spi_rx_size;
//	struct rt_semaphore spi_transport_sem;
//	struct rt_mutex spi3_lock;
//	uint8_t spi_status;
//	uint8_t spi_tx_buf[4104];
//	uint8_t spi_rx_buf[4104];
//	CM_DMA_TypeDef *cm_dma_tx;
//	CM_DMA_TypeDef *cm_dma_rx;
//	CM_SPI_TypeDef *spi_cm;
//}Spi_Exchange_Def;

extern Spi_Transport_Def spi1_obj;

//SPIx Configs
void App_SPIxCfg(void);
// 初始化SPI对象
rt_err_t Init_Spi_Obj(Spi_Transport_Def* obj, char* name, CM_SPI_TypeDef *SPI_CMx, CM_DMA_TypeDef *CM_DMAx, uint8_t DMAx_CH);

rt_err_t Spi_Start_Wait_Interface(Spi_Transport_Def* obj);
// 设置SPI速度
rt_err_t Spi_Set_Band_Interface(Spi_Transport_Def *obj, uint32_t u32BaudRatePrescaler);


#endif /* __BSP_SPI_INIT_HELPER_H__ */

/*eof*/
